from google.appengine.ext import db
from google.appengine.ext.db import polymodel

class Element(polymodel.PolyModel):
  owner = db.UserProperty()
  label = db.StringProperty()
  next_element= db.SelfReferenceProperty() 
  id = db.IntegerProperty()
  version = db.StringProperty()

  def delete(self, delete_all=False):
    if delete_all:
      if self.next_element:
        self.next_element.delete(delete_all=delete_all)
    else:
      for e in self.element_set:
        e.next_element = self.next_element
        e.put()
      for g in self.group_set:
        g.first_element = self.next_element
        g.put()
    polymodel.PolyModel.delete(self)

class Password(Element):
  username = db.StringProperty()
  notes = db.StringProperty()
  password = db.StringProperty()

class Group(Element):
  first_element = db.ReferenceProperty(Element)
  master_pass = db.StringProperty()

  def delete(self):
    '''Deletes a group and all the elements inside'''
    if self.first_element:
      self.first_element.delete(delete_all=True)
    Element.delete(self)

class Message(db.Model):
  owner = db.UserProperty()
  id = db.IntegerProperty()
  action = db.IntegerProperty() # An enum of different types of actions
  element = db.ReferenceProperty(Element)
